package xy.donate.android.ui.activity;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.room.Room;

import xy.donate.android.R;
import xy.donate.android.aop.SingleClick;
import xy.donate.android.common.MyActivity;
import xy.donate.android.db.LoginDB;
import xy.donate.android.db.LoginDao;
import xy.donate.android.entity.UserLoginEntity;
import xy.donate.android.helper.InputTextHelper;
import xy.donate.android.http.model.HttpData;
import xy.donate.android.http.request.GetCodeApi;
import xy.donate.android.http.request.VerifyCodeApi;
import xy.donate.android.http.response.LoginBean;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.hjq.widget.view.CountdownView;

/**
 * author : Xy
 * desc   : 忘记密码
 */
public final class PasswordForgetActivity extends MyActivity {

    private EditText mPhoneView;
    private EditText mCodeView;
    private CountdownView mCountdownView;
    private Button mCommitView;

    private LoginDao loginDao;

    @Override
    protected int getLayoutId() {
        return R.layout.password_forget_activity;
    }

    @Override
    protected void initView() {
        mPhoneView = findViewById(R.id.et_password_forget_phone);
        mCodeView = findViewById(R.id.et_password_forget_code);
        mCountdownView = findViewById(R.id.cv_password_forget_countdown);
        mCommitView = findViewById(R.id.btn_password_forget_commit);
        setOnClickListener(mCountdownView, mCommitView);

        InputTextHelper.with(this)
                .addView(mPhoneView)
                .addView(mCodeView)
                .setMain(mCommitView)
                .build();

        loginDao = Room.databaseBuilder(this, LoginDB.class,"LoginDB")
                .allowMainThreadQueries()
                .build().loginDao();
    }

    @Override
    protected void initData() {

    }

    @SingleClick
    @Override
    public void onClick(View v) {
        if (v == mCountdownView) {
            if (mPhoneView.getText().toString().length() != 11) {
                toast(R.string.common_phone_input_error);
                return;
            }
            toast(R.string.common_code_send_hint);
            mCountdownView.start();
        } else if (v == mCommitView) {

            if (mPhoneView.getText().toString().length() != 11) {
                toast(R.string.common_phone_input_error);
                return;
            }

            if (mCodeView.getText().toString().length() != getResources().getInteger(R.integer.sms_code_length)) {
                ToastUtils.show(R.string.common_code_error_hint);
                return;
            }

            UserLoginEntity bean = loginDao.queryPhone(mPhoneView.getText().toString());
            if (bean == null){
                toast("手机号还没注册!");
                return;
            }
            PasswordResetActivity.start(getActivity(), mPhoneView.getText().toString(), mCodeView.getText().toString());
            finish();
        }
    }
}